package se.webbzon.boltzmann.login;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;

import se.webbzon.oschi01.aes.HexOutputStream;

public class LoginFrame extends Login {
	
	// The frame of this login frame.
	private final FrameImpl frame;
	
	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a new login frame. **/
	public LoginFrame() {
		frame = new FrameImpl();
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public void show() {
		frame.setVisible(true);
	}

	@Override public void dispose() {
		frame.dispose();
	}

	@Override public void setUsername(String username) {
		super.setUsername(username);
		frame.username.setText(username);
	}

	@Override public void setHashword(int passwordLength, byte[] hashword) {
		super.setHashword(passwordLength, hashword);
		frame.password.setText(new String(new char[passwordLength]).replace("\0", "*"));
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private final class FrameImpl extends JFrame {
		
		/**
		 * 
		 */
		private static final long serialVersionUID = -8872369755076241380L;

		// The username field of this frame
		private final JTextField username;
		
		// The password field of this frame
		private final JPasswordField password;
		
		public FrameImpl() {
			setPreferredSize(new Dimension(400,96));
			setResizable(false);
			setUndecorated(true);
			setAlwaysOnTop(true);
			setOpacity(0.90f);
			JPanel panel = new JPanel();
			panel.setBackground(new Color(220,220,255));
			panel.setLayout(new GridLayout(4,2));
			panel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
			username = new JTextField();
			password = new JPasswordField();
			panel.add(new JLabel("Login at "));
			panel.add(new JLabel("www.haffy-entertainment.com"));
			panel.add(new JLabel("Username:"));
			panel.add(username);
			panel.add(new JLabel("Password"));
			panel.add(password);
			panel.add(new JButton(new CancelAction()));
			panel.add(new JButton(new LoginAction()));
			add(panel);
			pack();
			setLocationRelativeTo(null);
		}
		
	}
	
	private final class LoginAction extends AbstractAction {
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 3600192649715419279L;

		public LoginAction() {
			putValue(AbstractAction.NAME, "Login");
		}

		@Override public void actionPerformed(ActionEvent arg0) {
			String uname = frame.username.getText();
			setUsername(uname);
			char[] pchars = frame.password.getPassword();
			byte[] hashword = createHashword(uname,String.valueOf(pchars));
			setHashword(pchars.length,hashword);
			for (int i = 0; i < pchars.length; i++)
				pchars[i] = '0';
			login();
		}
		
	}
	
	private final class CancelAction extends AbstractAction {

		/**
		 * 
		 */
		private static final long serialVersionUID = 5347633312408293565L;

		public CancelAction() {
			putValue(AbstractAction.NAME, "Cancel");
		}

		@Override public void actionPerformed(ActionEvent arg0) {
			cancel();
			dispose();
		}
		
	}

}

	

	
